package com.san.app.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.app.domain.InspectTemplateDO;
import com.san.app.domain.InspectTemplateDTO;
import com.san.app.service.InspectTemplateService;
import com.san.app.vo.InspectTemplateVO;
import com.san.app.vo.InspectVO;
import com.san.base.controller.BaseController;
import com.san.common.annotation.Log;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;
import com.san.metadata.domain.ColumnDTO;
import com.san.metadata.service.ManageMetadataService;
/**
 * 信用核查模板控制层
 * @author SuperSR
 * @date 2019年3月27日 下午2:24:00
 *
 */
@Controller
@RequestMapping("/app/inspecttemplate")
public class InspectTemplateController extends BaseController{

	@Autowired
	private InspectTemplateService inspectTemplateService;
	@Autowired
	private ManageMetadataService manageMetadataService;
	
	private static final Logger logger = LoggerFactory.getLogger(InspectTemplateController.class);
	
	private String PREFIX = "app/inspecttemplate";
	
	@GetMapping("/view/{inspectId}/{inspectType}")
	@RequiresPermissions("app:inspecttemplate:view")
	String view(Model model,@PathVariable("inspectId") Long inspectId,@PathVariable("inspectType") int inspectType) {
		model.addAttribute("inspectId",inspectId);
		model.addAttribute("inspectType",inspectType);
		return PREFIX + "/view";
	}
	
	@GetMapping("/list")
	@RequiresPermissions("app:inspecttemplate:view")
	@ResponseBody
	public PageUtils list(@RequestParam Map<String, Object> params) {
		PageUtils pageUtils = null;
		try {
			Query query = new Query(params);
			List<InspectTemplateDTO> list = inspectTemplateService.getTemplateList(query);
			for (InspectTemplateDTO inspectTemplateDTO : list) {
				String[] columns =  inspectTemplateDTO.getColumnId().split(",");
				StringBuilder sb = toColumn(columns);
				inspectTemplateDTO.setColumnName(sb.toString());
			}
			//int total = .count(query);
			pageUtils = new PageUtils(list, list.size());
		} catch (Exception e) {
			logger.error("获取信用核查模板列表出错",e);
		}
		return pageUtils;
	}
	
	/**
	 * 进入新增页面
	 * @return
	 */
	@GetMapping("/add/{inspectId}/{inspectType}")
	@RequiresPermissions("app:inspecttemplate:add")
	String add(Model model,@PathVariable("inspectId") Long inspectId,@PathVariable("inspectType") int inspectType) {
		model.addAttribute("inspectId",inspectId);
		model.addAttribute("inspectType",inspectType);
		return PREFIX + "/add";
	}
	
	/**
	 * 保存核查项
	 * @param inspectTemplateVO
	 * @return
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("app:inspecttemplate:add")
	@Log("新增信用核查核查项")
	public R save(InspectTemplateVO inspectTemplateVO) {
		try{
			String[] columns =  inspectTemplateVO.getColumnId().split(",");
			StringBuilder sb = toColumn(columns);
			inspectTemplateVO.setColumnName(sb.toString());
			int state = inspectTemplateService.save(inspectTemplateVO);
			if(state > 0){
				return R.ok();
			}
		} catch(Exception e) {
			logger.error("新增信用核查失败",e);
		}
		return R.error();
	}
	
	/**
	 * 进入编辑页面
	 * @return
	 */
	@GetMapping("/edit/{templateId}/{inspectId}/{inspectType}")
	@RequiresPermissions("app:inspecttemplate:edit")
	String edit(Model model,@PathVariable("templateId") Long templateId,@PathVariable("inspectId") Long inspectId,@PathVariable("inspectType") int inspectType) {
		try{
		model.addAttribute("inspectId",inspectId);
		model.addAttribute("inspectType",inspectType);
		InspectTemplateDTO templateDTO = inspectTemplateService.getById(templateId);
			String[] columns =  templateDTO.getColumnId().split(",");
			StringBuilder sb = toColumn(columns);
			templateDTO.setColumnName(sb.toString());
		model.addAttribute("templateDTO",templateDTO);
		}catch(Exception e){
			logger.error("获取核查明细字段出错",e);
		}
		return PREFIX + "/edit";
	}

	/**
	 * 将字段id转为字段名称字符串
	 * @param columns
	 * @return
	 * @throws Exception
	 */
	private StringBuilder toColumn(String[] columns) throws Exception {
		StringBuilder sb = new StringBuilder("");
		for (int i = 0; i < columns.length; i++) {
			Long columnId = Long.parseLong(columns[i]);
			ColumnDTO columnDTO =  manageMetadataService.getColumnById(columnId);
			if(i == 0){
				sb.append(columnDTO.getColumnName());
			}else{
				sb.append("," + columnDTO.getColumnName());
			}
		}
		return sb;
	}
	
	/**
	 * 修改核查项
	 * @param inspectTemplateVO
	 * @return
	 */
	@ResponseBody
	@PostMapping("/doEdit")
	@RequiresPermissions("app:inspecttemplate:edit")
	@Log("修改信用核查核查项")
	public R doEdit(InspectTemplateVO inspectTemplateVO) {
		try{
			String[] columns =  inspectTemplateVO.getColumnId().split(",");
			StringBuilder sb = toColumn(columns);
			inspectTemplateVO.setColumnName(sb.toString());
			int state = inspectTemplateService.doEdit(inspectTemplateVO);
			if(state > 0){
				return R.ok();
			}
		} catch(Exception e) {
			logger.error("修改信用核查失败",e);
		}
		return R.error();
	}
	
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("app:inspecttemplate:remove")
	@Log("删除信用核查核查项")
	public R remove(Long id){
		try{
			int state = inspectTemplateService.remove(id);
			if(state > 0){
				return R.ok();
			}
		} catch(Exception e) {
			logger.error("删除信用核查核查项失败",e);
		}
		return R.error();
	}
}







